package com.xbs.dao.content;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.RowDatas;
/**
 * 角色管理dao
 * @author 余凯
 *
 */
import com.xbs.entity.RoleEntity;
import com.xbs.entity.RoleLimitEntity;
public class RoleAdminDao extends BaseDao {
	private RowDatas<RoleEntity> re;
	private RowDatas<String>  str;
	public RoleAdminDao() {
		re = new getRole();
		str = new getStr();
	}
	
	/**
	 * 1.获取数据库中所有的角色
	 * @return
	 */
	public List<RoleEntity> selRole(){
		String sql = "select * from tb_role";
		return queryList(sql, re);
	}
	
	/**
	 * 2.先删除子表中该角色的权限
	 * @param id
	 * @return
	 */
	public int delChild(int id){
		String sql = "delete  from tb_role_limit where role_id='"+id+"'";
		return updateExecuted(sql);
	}
	
	/**
	 * 3.根据角色id，删除该角色（父表）
	 * @param id
	 * @return
	 */
	public int del(int id){
		String sql="delete  from tb_role where role_id='"+id+"'";
		return updateExecuted(sql);
	}
	
	
	/**
	 * 4.添加新的角色
	 * @param name
	 * @return
	 */
	public int ins(String name){
		String sql="insert into tb_role (role_name) values ('"+name+"')";
		return updateExecuted(sql);
	}
	
	/**
	 * 5.修改角色名称
	 * @param yname
	 * @param uname
	 * @return
	 */
	public int up(String yname,String uname){
		String sql="update tb_role set role_name='"+uname+"' where role_name='"+yname+"'";
		return updateExecuted(sql);
	}
	
	/**
	 * 6.删除角色时，将对应员工的角色清空
	 * @param role
	 * @return
	 */
	public int upemp(String role){
		String sql = "update tb_employee set emp_role=''  where emp_role='"+role+"'";
		System.out.println(sql);
		return updateExecuted(sql);
	}
	
	/**
	 * 7.根据角色id查找角色姓名
	 * @param id
	 * @return
	 */
	public String selR(int id){
		String sql = "select role_name from tb_role where role_id='"+id+"'";
		System.out.println(sql);
		return query(sql, str);
	}
	
	public class getRole implements RowDatas<RoleEntity>{
		@Override
		public RoleEntity getRowData(ResultSet rs) throws SQLException {
			RoleEntity re = new RoleEntity();
			re.setRoleid(rs.getInt("role_id"));
			re.setRolename(rs.getString("role_name"));
			return re;
		}
	}
	
	public class getStr implements RowDatas<String>{
		@Override
		public String getRowData(ResultSet rs) throws SQLException {
			return rs.getString("role_name");
		}
	}
	
}
